package Admin;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Vector;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JCheckBox;

public class QnA extends JPanel {
	JPanel contentPane;
	private JTextField textField;
	private JButton btnNewButton;
	private JComboBox comboBox;
	private JScrollPane scrollPane;
	private JTable table;
	private JLabel lblNewLabel;
	private JLabel label;
	private JScrollPane scrollPane_1;
	private JTextArea textArea;
	private JLabel lblZkxprhfl;
	private JLabel label_1;
	private JButton btnNewButton_1;
	private JLabel mid;
	private JLabel wdate;
	private JLabel category;
	private JLabel subject;
	private JRadioButton incomplete;
	private JRadioButton complete;
	private JRadioButton all;
	private JLabel label_9;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private final ButtonGroup buttonGroup_1 = new ButtonGroup();
	private JLabel label_8;
	private JLabel mail;
	ManageDTO dto = new ManageDTO();
	private JCheckBox confirm;
	private JCheckBox sms;
	private JButton viewre;
	QnAVo v;
	String [] s_category = new String[] {"전체", "카테고리", "제목", "작성자"};
	String selctcategory="";
	String selectcondition="";
	
	/**
	 * Create the panel.
	 */
	public QnA(JPanel contentPane) {
		setBackground(Color.WHITE);
		this.contentPane = contentPane;
		setLayout(null);
		add(getTextField());
		add(getBtnNewButton());
		add(getComboBox());
		add(getScrollPane());
		add(getLblNewLabel());
		add(getLabel());
		add(getScrollPane_1());
		add(getLblZkxprhfl());
		add(getLabel_1_1());
		add(getBtnNewButton_1());
		add(getMid());
		add(getWdate());
		add(getCategory());
		add(getSubject());
		add(getIncomplete());
		add(getComplete());
		add(getAll());
		add(getLabel_9());
		add(getLabel_8());
		add(getMail());
		add(getConfirm());
		add(getSms());
		add(getViewre());
	}
	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setBounds(101, 74, 253, 21);
			textField.setColumns(10);
		}
		return textField;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.setBackground(Color.WHITE);
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					
					select();
					
				}
			});
			btnNewButton.setFont(new Font("Gulim", Font.PLAIN, 12));
			btnNewButton.setBounds(366, 73, 97, 23);
		}
		return btnNewButton;
	}
	private JComboBox getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox();
			comboBox.setFont(new Font("Gulim", Font.PLAIN, 12));
			comboBox.setModel(new DefaultComboBoxModel(s_category));
			comboBox.setBounds(12, 74, 77, 21);
		}
		return comboBox;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 105, 522, 407);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent arg0) {
					btnNewButton_1.setEnabled(true);
					dto = new ManageDTO();
					
					v = dto.selectoneQnA((Integer)table.getValueAt(table.getSelectedRow(), 0));
					
					mid.setText(v.getMid());
					category.setText(v.getCategory());
					mail.setText(v.getMail());
					subject.setText(v.getSubject());
					textArea.setText(v.getContent());
					wdate.setText(v.getWdate().toString());
					
					
					
					if(v.getConfirm().equals("y")){
						confirm.setSelected(true);
						viewre.setForeground(Color.RED);
						viewre.setEnabled(true);
						
					}else{
						confirm.setSelected(false);
						viewre.setEnabled(false);
					}
					
					if(v.getSnsreply().equals("true")){
						sms.setSelected(true);
					}else{
						sms.setSelected(false);
					}
										
				}
			});
		}
		return table;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("고객ID : ");
			lblNewLabel.setFont(new Font("Gulim", Font.PLAIN, 12));
			lblNewLabel.setBounds(565, 90, 68, 21);
		}
		return lblNewLabel;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("제목 : ");
			label.setFont(new Font("Gulim", Font.PLAIN, 12));
			label.setBounds(565, 203, 68, 21);
		}
		return label;
	}
	private JScrollPane getScrollPane_1() {
		if (scrollPane_1 == null) {
			scrollPane_1 = new JScrollPane();
			scrollPane_1.setBounds(565, 234, 261, 207);
			scrollPane_1.setViewportView(getTextArea());
		}
		return scrollPane_1;
	}
	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
		}
		return textArea;
	}
	private JLabel getLblZkxprhfl() {
		if (lblZkxprhfl == null) {
			lblZkxprhfl = new JLabel("카테고리 : ");
			lblZkxprhfl.setFont(new Font("Gulim", Font.PLAIN, 12));
			lblZkxprhfl.setBounds(565, 176, 68, 21);
		}
		return lblZkxprhfl;
	}
	private JLabel getLabel_1_1() {
		if (label_1 == null) {
			label_1 = new JLabel("작성일 : ");
			label_1.setFont(new Font("Gulim", Font.PLAIN, 12));
			label_1.setBounds(565, 119, 68, 21);
		}
		return label_1;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("답변하기");
			btnNewButton_1.setBackground(Color.WHITE);
			btnNewButton_1.setEnabled(false);
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					QnAMail q = new QnAMail(v);
					q.setVisible(true);
					
				}
			});
			btnNewButton_1.setFont(new Font("Gulim", Font.PLAIN, 12));
			btnNewButton_1.setBounds(740, 461, 86, 23);
		}
		return btnNewButton_1;
	}
	private JLabel getMid() {
		if (mid == null) {
			mid = new JLabel("");
			mid.setFont(new Font("Gulim", Font.PLAIN, 12));
			mid.setBounds(627, 90, 111, 21);
		}
		return mid;
	}
	private JLabel getWdate() {
		if (wdate == null) {
			wdate = new JLabel("");
			wdate.setFont(new Font("Gulim", Font.PLAIN, 12));
			wdate.setBounds(625, 118, 166, 21);
		}
		return wdate;
	}
	private JLabel getCategory() {
		if (category == null) {
			category = new JLabel("");
			category.setFont(new Font("Gulim", Font.PLAIN, 12));
			category.setBounds(628, 176, 166, 21);
		}
		return category;
	}
	private JLabel getSubject() {
		if (subject == null) {
			subject = new JLabel("");
			subject.setFont(new Font("Gulim", Font.PLAIN, 12));
			subject.setBounds(608, 203, 267, 21);
		}
		return subject;
	}
	private JRadioButton getIncomplete() {
		if (incomplete == null) {
			incomplete = new JRadioButton("미 완료만 보기");
			incomplete.setBackground(Color.WHITE);
			buttonGroup.add(incomplete);
			incomplete.setFont(new Font("굴림", Font.PLAIN, 12));
			incomplete.setBounds(287, 32, 121, 23);
		}
		return incomplete;
	}
	private JRadioButton getComplete() {
		if (complete == null) {
			complete = new JRadioButton("완료만 보기");
			complete.setBackground(Color.WHITE);
			buttonGroup.add(complete);
			complete.setFont(new Font("굴림", Font.PLAIN, 12));
			complete.setBounds(183, 31, 97, 23);
		}
		return complete;
	}
	private JRadioButton getAll() {
		if (all == null) {
			all = new JRadioButton("전체보기");
			all.setBackground(Color.WHITE);
			buttonGroup.add(all);
			all.setSelected(true);
			all.setFont(new Font("굴림", Font.PLAIN, 12));
			all.setBounds(97, 32, 75, 23);
		}
		return all;
	}
	private JLabel getLabel_9() {
		if (label_9 == null) {
			label_9 = new JLabel("답변 조건  :");
			label_9.setFont(new Font("Gulim", Font.PLAIN, 12));
			label_9.setBounds(12, 36, 82, 15);
		}
		return label_9;
	}
	private JLabel getLabel_8() {
		if (label_8 == null) {
			label_8 = new JLabel("수신 메일 : ");
			label_8.setFont(new Font("Gulim", Font.PLAIN, 12));
			label_8.setBounds(566, 145, 67, 21);
		}
		return label_8;
	}
	private JLabel getMail() {
		if (mail == null) {
			mail = new JLabel("");
			mail.setFont(new Font("Gulim", Font.PLAIN, 12));
			mail.setBounds(635, 149, 191, 21);
		}
		return mail;
	}
	
	
	public void select(){
		Vector<String> vec = new Vector<String>();
		
		vec.add("No");
		vec.add("ID");
		vec.add("카테고리");
		vec.add("제목");
		vec.add("답변");
		vec.add("작성일");
		
		DefaultTableModel tmodel = new DefaultTableModel(vec, 0);
		dto = new ManageDTO();
		
		switch(comboBox.getSelectedIndex()){
		case 0 : selctcategory = s_category[0];
			break;
		case 1 : selctcategory = s_category[1];
			break;
		case 2 : selctcategory = s_category[2];
			break;
		case 3 : selctcategory = s_category[3];
			break;
		
		}
		
		
		if(all.isSelected()){
			selectcondition = "all";
		}else if(complete.isSelected()){
			selectcondition = "complete";
		}else if(incomplete.isSelected()){
			selectcondition = "incomplete";
		}
		
		
		
		ArrayList<QnAVo> al = dto.selectQnA(selctcategory, textField.getText(), selectcondition);
		
		for (QnAVo vo : al) {
			Vector v = new Vector();
			if(vo.getDeep().split("-").length == 1){
			v.add(vo.getRno());
			v.add(vo.getMid());
			v.add(vo.getCategory());
			v.add(vo.getSubject());
			v.add(vo.getConfirm());
			v.add(vo.getWdate());
			

			tmodel.addRow(v);
			}
		}

		table.setModel(tmodel);
		
		
		
		
		
	}
	private JCheckBox getConfirm() {
		if (confirm == null) {
			confirm = new JCheckBox("답변 여부");
			confirm.setBackground(Color.WHITE);
			confirm.setEnabled(false);
			confirm.setFont(new Font("Gulim", Font.PLAIN, 12));
			confirm.setBounds(564, 447, 77, 23);
		}
		return confirm;
	}
	private JCheckBox getSms() {
		if (sms == null) {
			sms = new JCheckBox("수신동의");
			sms.setBackground(Color.WHITE);
			sms.setEnabled(false);
			sms.setFont(new Font("굴림", Font.PLAIN, 12));
			sms.setBounds(564, 472, 73, 23);
		}
		return sms;
	}
	private JButton getViewre() {
		if (viewre == null) {
			viewre = new JButton("답변보기");
			viewre.setBackground(Color.WHITE);
			viewre.setEnabled(false);
			viewre.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ViewAnswer answer = new ViewAnswer((Integer)table.getValueAt(table.getSelectedRow(), 0),v.getMid());
					answer.setVisible(true);
					
					
					
				}
			});
			viewre.setFont(new Font("Gulim", Font.PLAIN, 12));
			viewre.setBounds(651, 460, 85, 23);
		}
		return viewre;
	}
}
